//
//  ZYWelcomeViewController.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/13.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit
import SDWebImage


class ZYWelcomeViewController: UIViewController {

    // 懒加载背景图片
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像
    private lazy var headImageView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named:"avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        // 设置头像
        if let url = ZYUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
           imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        return imageView
        
    }()
    // 欢迎信息
    private lazy var messageLbale:UILabel = {
       
        let lable = UILabel()
        // 设置欢迎信息
        if let name = ZYUserAccountViewModel.sharedUserAccount.userAccount?.name {
             lable.text = name + " , 欢迎回来!"
        } else {
             lable.text = "欢迎回来"
        }
        lable.font = UIFont.systemFontOfSize(14)
        lable.textColor = UIColor.darkGrayColor()
        return lable
    }()
    
    
    // 自定义view
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    // 添加子视图设置约束
    private func setUpUI() {
        
        view.addSubview(headImageView)
        view.addSubview(messageLbale)
        // 设置约束
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        
        messageLbale.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }
    }

    // 首页动画
    private func startAnimation() {
        
        // 改变约束
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        messageLbale.alpha = 0
        // initialSpringVelocity 初始速度
        // usingSpringWithDamping 阻尼
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            // 让子视图重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLbale.alpha = 1
                    }, completion: { (_) -> Void in
                       
                        // 动画执行完毕后进入首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: self)
                        
                })
        }
        
    }
    
    
}
